import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'

import SysPageColumn from '@main/entities/service/admin/sys/sys-page-column.entity'
import { Repository } from 'typeorm'

@Injectable()
export class SysPageColumnService {
  constructor(
    @InjectRepository(SysPageColumn)
    private pageColumnRepository: Repository<SysPageColumn>,
  ) { }

  async list(data,uid) {
    const { url } = data

    const list = await this.pageColumnRepository.find({ where: { userId: uid, url } })

    return list
  }

  async update(list,uid) {
    if (list && list.length) {
      const url = list[0].url

      await this.pageColumnRepository.delete({ userId: uid, url })

      for (const item of list)
        item.userId = uid

      await this.pageColumnRepository.insert(list)
    }
  }
}
